<?php

namespace App\Exports;

use App\Models\Talent;
use Maatwebsite\Excel\Concerns\FromCollection;

class WaitingSecondListExport implements FromCollection
{
    public function collection()
    {
        $talent = new Talent();
        $request = request();
        $talent = $talent->newQuery();
        $talent->where('interview_status', 5)->where('offer_status', 0);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('wait_status') || $request->input('wait_status') === 0 || $request->input('wait_status') === '0') {
            $talent->where('wait_status', $request->input('wait_status'));
        }
        if ($request->input('batch')) {
            $talent->where('batch', $request->input('batch'));
        }
        if ($request->input('sex') || $request->input('sex') === 0 || $request->input('sex') === '0') {
            $talent->where('sex', $request->input('sex'));
        }

        $lists = $talent->get();
        return $this->transform($lists);
    }

    private function transform($lists)
    {
        $lists->transform(function ($item) {
            return [
                'name' => $item->name,
                'recommend' => $item->recommend,
                'sex_name' => (int) $item->sex === 0 ? '男' : '女',
                'birthday' => $item->birthday,
                'education' => $item->education,
                'university' => $item->university,
                'profession' => $item->profession,
                'interview_status_name' => Talent::INTERVIEW_STATUS_NAME[(int) $item->interview_status],
                'wait_status2_name' => Talent::WAIT_STATUS_NAME[(int) $item->wait_status2],
                'offer_status_name' => Talent::OFFER_STATUS_NAME[(int) $item->offer_status],
            ];
        });

        $arr = $lists->all();
        $title = [
            '姓名', '推荐职位', '性别', '出生日期', '学历', '学校', '专业',
            '面试状态', '待面试状态', '录用状态',
        ];
        array_unshift($arr, $title);

        return collect($arr);
    }
}
